/*
 * @Author: wurangkun
 * @Date: 2024-05-22 11:45:25
 * @LastEditTime: 2024-05-22 18:01:39
 * @LastEditors: wurangkun
 * @FilePath: \flutter_example\lib\components\KInput.dart
 * @Description: 
 */
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class KInput extends StatefulWidget {
  final String hintText;
  final String? defaultText;
  final Function(String)? onChanged;
  final Function(String)? onSubmitted;

  const KInput(
      {super.key,
      this.hintText = '请输入',
      this.defaultText,
      this.onChanged,
      this.onSubmitted});

  @override
  State<KInput> createState() => _KInputState();
}

class _KInputState extends State<KInput> {
  final TextEditingController _textEditingController = TextEditingController();

  @override
  void initState() {
    super.initState();
    if (widget.defaultText != null) {
      _textEditingController.value = TextEditingValue(
          text: widget.defaultText as String,
          selection: TextSelection.fromPosition(TextPosition(
              affinity: TextAffinity.downstream,
              offset: (widget.defaultText as String).length)));
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        alignment: Alignment.center,
        padding: EdgeInsets.fromLTRB(20.w, 0, 20.w, 0),
        height: 120.h,
        decoration: BoxDecoration(
            color: const Color(0xFFf6f7f9),
            borderRadius: BorderRadius.all(Radius.circular(60.h))),
        child: SizedBox(
          child: NormalInput(
            controller: _textEditingController,
            hintText: widget.hintText,
            onChanged: widget.onChanged,
            onSubmitted: widget.onSubmitted,
          ),
        ));
  }
}

class NormalInput extends StatefulWidget {
  final String hintText;
  final Function(String)? onChanged;
  final Function(String)? onSubmitted;

  final TextEditingController controller;
  const NormalInput(
      {super.key,
      this.hintText = '请输入',
      required this.controller,
      this.onChanged,
      this.onSubmitted});

  @override
  State<NormalInput> createState() => _NormalInputState();
}

class _NormalInputState extends State<NormalInput> {
  @override
  Widget build(BuildContext context) {
    return TextField(
      keyboardType: TextInputType.text,
      controller: widget.controller,
      onChanged: widget.onChanged,
      onSubmitted: widget.onSubmitted,
      style: const TextStyle(
          fontSize: 20, color: Color(0xff333333), fontWeight: FontWeight.bold),
      decoration: InputDecoration(
          hintText: widget.hintText,
          hintStyle: const TextStyle(
              fontSize: 20,
              color: Color(0x2b333333),
              fontWeight: FontWeight.w500),
          border: InputBorder.none,
          contentPadding: const EdgeInsets.all(0)),
    );
  }
}
